﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Data;
using System.Text;

using Dropthings.Business.Facade;
using InfoSoftGlobal;
using Dropthings.Widget.Framework;
using Dropthings.Business.Facade.Context;

public partial class Widgets_FusionChart_AmmeterRankByBuildingArea : System.Web.UI.UserControl, IWidget
{
    #region Fields

    private IWidgetHost _Host;
    private XElement _State;
    private XElement State
    {
        get
        {
            if (_State == null)
            {
                string stateXml = this._Host.GetState();
                if (string.IsNullOrEmpty(stateXml))
                {
                    _State = new XElement("state",
                        new XElement("year", DateTime.Now.Year.ToString()),
                        new XElement("month", DateTime.Now.Month.ToString()),
                        new XElement("type", 1));
                }
                else
                {
                    _State = XElement.Parse(stateXml);
                }
            }
            return _State;
        }
    }

    public int ShowYear
    {
        get { return State.Element("year") == null ? DateTime.Now.Year : int.Parse(State.Element("year").Value); }
        set
        {
            if (State.Element("year") == null)
                State.Add(new XElement("year", value));
            else
                State.Element("year").Value = value.ToString();
        }
    }

    public int ShowMonth
    {
        get { return (State.Element("month") == null || State.Element("month").Value == "0") ? DateTime.Now.Month : int.Parse(State.Element("month").Value); }
        set
        {
            if (State.Element("month") == null)
                State.Add(new XElement("month", value));
            else
                State.Element("month").Value = value.ToString();
        }

    }

    public int ShowType
    {
        get { return State.Element("type") == null ? 1 : int.Parse(State.Element("type").Value); }
        set
        {
            if (State.Element("type") == null)
                State.Add(new XElement("type", value));
            else
                State.Element("type").Value = value.ToString();
        }
    }

    #endregion Fields

    #region Methods

    void IEventListener.AcceptEvent(object sender, EventArgs e)
    {

    }

    void IWidget.Closed()
    {
    }

    void IWidget.Collasped()
    {
    }

    void IWidget.Expanded()
    {
    }

    void IWidget.HideSettings(bool userClicked)
    {
        SettingsPanel.Visible = false;
        if (userClicked)
        {
            ShowLinkBtn(true);
            this.ShowType = int.Parse(rblType.Value);
            if (rblType.Value == "1")
            {
                this.ShowYear = int.Parse(ddlYear.SelectedValue);
                this.ShowMonth = 0;
            }
            else if (rblType.Value == "2")
            {
                this.ShowYear = int.Parse(ddlYear.SelectedValue);
                this.ShowMonth = int.Parse(ddlMonth.SelectedValue);
            }
        }
        this.SaveState();
    }

    void IWidget.Init(IWidgetHost host)
    {
        this._Host = host;
    }

    void IWidget.Maximized()
    {
    }

    void IWidget.Restored()
    {
    }

    void IWidget.ShowSettings(bool userClicked)
    {
        SettingsPanel.Visible = true;
        if (userClicked)
        {
            ShowDDLControl(rblType.Value);
            ShowLinkBtn(false);
            if (rblType.Value == "1")
            {
                if (ddlYear.Items.Count == 0)
                {
                    for (int i = 2000; i <= DateTime.Now.Year; i++)
                    {
                        ddlYear.Items.Add(new ListItem(i.ToString()));
                    }
                }
                ddlYear.SelectedIndex = -1;
                ddlYear.Items.FindByText(this.ShowYear.ToString()).Selected = true;
            }
            else if (rblType.Value == "2")
            {
                if (ddlYear.Items.Count == 0)
                {
                    for (int i = 2000; i <= DateTime.Now.Year; i++)
                    {
                        ddlYear.Items.Add(new ListItem(i.ToString()));
                    }
                }
                ddlYear.SelectedIndex = -1;
                ddlYear.Items.FindByText(this.ShowYear.ToString()).Selected = true;
                ddlMonth.SelectedIndex = -1;
                ddlMonth.Items.FindByValue(this.ShowMonth.ToString()).Selected = true;
            }
        }
    }

    private void SaveState()
    {
        var xml = this.State.Xml();
        this._Host.SaveState(xml);
        this._State = null;
    }

    private void ShowDDLControl(string type)
    {
        tdMonth.Visible = type == "2" ? true : false;
    }

    private void ShowLinkBtn(bool b)
    {
        lbtnYear.Enabled = b;
        lbtnMonth.Enabled = b;
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        if (string.IsNullOrEmpty(rblType.Value))
            rblType.Value = this.ShowType.ToString();
        SetLinkBtnStyle(rblType.Value);
        ShowChartControl(rblType.Value);
    }

    private void SetLinkBtnStyle(string str)
    {
        if (str == "1")
        {
            lbtnYear.Attributes["style"] = "color:red;text-decoration:underline;";
            lbtnMonth.Attributes.Remove("style");
        }
        else if (str == "2")
        {
            lbtnMonth.Attributes["style"] = "color:red;text-decoration:underline;";
            lbtnYear.Attributes.Remove("style");
        }
    }

    private void ShowChartControl(string type)
    {
        if (type == "1")
        {
            GetYearChartData();
        }
        else if (type == "2")
        {
            GetMonthChartData();
        }
    }

    private void GetYearChartData()
    {
        DataSet ds = null;
        using (Facade facade = new Facade(AppContext.GetContext(Context)))
        {
            ds = facade.GetAmmeterRankByBuildingArea(ShowYear, -1);
        }
        StringBuilder sb = new StringBuilder();
        ltTitle.Text = string.Format("{0}年面积用电情况排名(单位:千瓦时)", ShowYear);
        sb.AppendFormat("<chart caption='' subcaption='' showValues='1' numberItemsPerPage='8' animation='1' lineAlpha='85' " +
                "baseFontColor='666666'  labelDisplay='rotate' useRoundEdges='1' decimals='2' formatNumberScale='0'>", ShowYear);
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sb.AppendFormat("<set label='{0}' value='{1}' />", dr[1], dr[2]);
            }
        }
        sb.Append("</chart>");
        Literal1.Text = FusionCharts.RenderChartHTML("../FusionCharts/SSGrid.swf", "", sb.ToString(), "myFirst", "100%", "200", false, false);
    }

    private void GetMonthChartData()
    {
        DataSet ds = null;
        using (Facade facade = new Facade(AppContext.GetContext(Context)))
        {
            ds = facade.GetAmmeterRankByBuildingArea(ShowYear, ShowMonth);
        }
        StringBuilder sb = new StringBuilder();
        ltTitle.Text = string.Format("{0}年{1}月面积用电情况排名(单位:千瓦时)", ShowYear, ShowMonth);
        sb.Append("<chart caption='' subcaption='' showValues='1' numberItemsPerPage='8' animation='1' lineAlpha='85'" +
                "baseFontColor='666666' labelDisplay='rotate' useRoundEdges='1' decimals='2' formatNumberScale='0'>");
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sb.AppendFormat("<set label='{0}' value='{1}' />", dr[1], dr[2]);
            }
        }
        sb.Append("</chart>");
        Literal1.Text = FusionCharts.RenderChartHTML("../FusionCharts/SSGrid.swf", "", sb.ToString(), "myFirst", "100%", "200", false, false);
    }

    protected void SaveSettings_Click(object sender, EventArgs e)
    {
        _Host.HideSettings(true);
    }

    protected void lbtnYear_Click(object sender, EventArgs e)
    {
        rblType.Value = "1";
        this._Host.Refresh(this);
    }

    protected void lbtnMonth_Click(object sender, EventArgs e)
    {
        rblType.Value = "2";
        this._Host.Refresh(this);
    }

    #endregion
}